import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/util/util.dart';
import 'package:get/get.dart';

extension on List<Widget> {
  ///添加中间间隔线
  List<Widget> addVline(BuildContext context) {
    List<Widget> list = [];
    if (length > 1) {
      for (var i = 0; i < length; i++) {
        if (i == length - 1) {
          list.add(this[i]);
        } else {
          list.add(this[i]);
          list.add(MenuBottomBar.vline(context));
        }
      }
    } else {
      list = this;
    }
    return list;
  }
}

class MenuBottomBar extends StatelessWidget {
  final String title;
  final List<Widget>? left;
  final List<Widget>? right;
  const MenuBottomBar({super.key, required this.title, this.left, this.right});

  static Widget vline(BuildContext context) {
    return Container(
        decoration: BoxDecoration(border: Border(right: BorderSide(color: Theme.of(context).primaryColor))),
        width: 1,
        height: 35);
  }

  @override
  Widget build(BuildContext context) {
    double w = (Get.width - 200.w) / 2;
    var hasBorder = left?.isNotEmpty == true || right?.isNotEmpty == true;
    return Container(
      decoration: hasBorder ? BoxDecoration(border: Border.all(color: Theme.of(context).primaryColor)) : null,
      width: double.infinity,
      height: 35,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          if (left != null)
            SizedBox(
              width: w,
              child: Row(
                children: left!.map((e) => Expanded(child: e)).toList().addVline(context),
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              ),
            ),
          Container(
            width: 195.w,
            height: 35,
            alignment: Alignment.center,
            child: Text(
              title,
              textAlign: TextAlign.center,
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 22),
            ),
            decoration: BoxDecoration(image: DecorationImage(image: AssetImage('menu_bottom_bg'.img))),
          ),
          if (right != null)
            SizedBox(
                width: w,
                child: right!.length == 1
                    ? right!.first
                    : Row(
                        children: right!.map((e) => Expanded(child: e)).toList().addVline(context),
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      )),
        ],
      ),
    );
  }
}
